/**
 * Created by Administrator on 2017/5/11 0011.
 */

//敌机
function Enemy() {
    this.ele = null;
    this.hp = 1;         //血量
    this.speed = 5;     //速度
    this.score = 10;    //分数
    this.dieImg = null;
    //敌机爆炸图片
    // this.dieImg = {
    //     small: ["img/plane1_die1.png", "img/plane1_die2.png", "img/plane1_die3.png"],
    //     mid: ["img/plane2_die1.png", "img/plane2_die2.png", "img/plane2_die3.png", "img/plane2_die4.png"],
    //     big: ["img/plane3_die1.png", "img/plane3_die2.png", "img/plane3_die3.png", "img/plane3_die4.png", "img/plane3_die5.png", "img/plane3_die6.png"]
    // }
    //this.
    this.id = parseInt(Math.random() * 10000) + "";

    //初始化敌机
    this.init = function (size, hp, speed, score, dieImg) {
        this.ele = document.createElement("div");
        gameEngine.ele.appendChild(this.ele);
        this.ele.dieImg = dieImg;
        this.ele.className = size;  //飞机大小
        this.ele.hp = hp;        //血量
        this.ele.speed = speed;     //速度
        this.ele.score = score;    //分数
        gameEngine.enemys[this.id] = this;  //将当前的敌机对象添加到数组中
        this.ele.style.left = parseInt(Math.random() * (gameEngine.ele.offsetWidth - this.ele.offsetWidth)) + "px";
        this.ele.style.top = -this.ele.offsetHeight + "px";
        return this;
    };

    //敌机落下
    this.fall = function () {
        var that = this;
        this.ele.timer1 = setInterval(function () {
            var y = that.ele.offsetTop + that.speed;
            if (y >= gameEngine.ele.offsetHeight) {
                that.ele.remove();
                clearInterval(that.ele.timer1);
                delete gameEngine.enemys[that.id];
            } else {
                that.ele.style.top = y + "px";
            }
        }, 50);
    }

    //敌机掉血
    this.hurt = function () {
        this.ele.hp--;
        //console.log(this.ele.hp);
        if (this.ele.hp == 0) {
            this.boom();

            //统计分数
            gameEngine.scoreDiv.innerHTML = gameEngine.scoreDiv.innerHTML - 0 + this.ele.score;
        }
    }

    //敌机爆炸
    this.boom = function () {
        clearInterval(this.ele.timer1); //碰撞后停止移动
        var arr = this.ele.dieImg;
        var n = 0;
        var that = this;
        var time2 = setInterval(function () {
            if (n >= arr.length) {
                that.ele.remove();
                clearInterval(time2);
                delete gameEngine.enemys[that.id];

            } else {
                that.ele.style.backgroundImage = "url(" + arr[n++] + ")";
            }

        }, 100);

    }


}

